package com.xhgj.thread;

/**
 * @author 周宇
 * @create 2020-06-27 19:46
 * 多线程购票 初始并发问题
 * 发现问题：多个线程操作同一个对象时，线程不安全，数据紊乱。
 */
public class BuyTickets implements Runnable {

    //票数
    private int tickeNums = 10;

    @Override
    public void run() {
        while (true) {
            if (tickeNums <= 0) {
                break;
            }
            //模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "-->拿到了第" + tickeNums-- + "票");
        }
    }

    public static void main(String[] args) {
        BuyTickets b = new BuyTickets();
        new Thread(b, "小周").start();
        new Thread(b, "肖鑫").start();
        new Thread(b, "黄牛").start();
        new Thread(b, "小明").start();
        new Thread(b, "肖红").start();
        new Thread(b, "萧兰").start();
    }
}
